package com.py.credit.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.py.credit.common.logs.ExceptionLogger;
import com.py.credit.constant.RecordStatusEnum;
import com.py.credit.dao.SContentDao;
import com.py.credit.dao.SContentInfoDao;
import com.py.credit.entity.Column;
import com.py.credit.entity.DataResult;
import com.py.credit.entity.SContentInfo;
import com.py.credit.entity.SysUser;
import com.py.credit.entity.TableResult;
import com.py.credit.entity.common.TreeObj;
import com.py.credit.service.ScontentService;
import com.py.credit.service.ShareResourceService;
import com.py.credit.service.TableService;
import com.py.credit.service.ZwcxContentService;
import com.py.framework.core.common.Page;

@Service
public class ZwcxContentServiceImpl implements ZwcxContentService {

	@Autowired
	private SContentDao sContentDao;
	
	@Autowired
	private TableService tableService;

	@Autowired
	private ShareResourceService shareResourceService;

	@Override
	public Map<String, Object> getDetail(String kId, String tybm, String scotnetType, SysUser userInfo) {

		Map<String, Object> result = new HashMap<String, Object>();

		List<TableResult> tableList = sContentDao.selectSsearchResource(kId, scotnetType);

//		// 一站式查询添加共享目录权限控制
//		if (null != userInfo && "ZWCX".equals(scotnetType)) {
//			Long topDeptId = userInfo.getOfficeId();
//			String pIds = userInfo.getAllParentId();
//			if (StringUtils.isNotBlank(pIds)) {
//				String t = StringUtils.split(pIds, ",")[0];
//				topDeptId = StringUtils.isNotBlank(t) ? Long.valueOf(t) : topDeptId;
//			}
//			List<String> resources = shareResourceService.getShareResource(topDeptId);
//			// 共享资源不为空
//			if (!CollectionUtils.isEmpty(resources) && !CollectionUtils.isEmpty(tableList)) {
//				for (Iterator<TableResult> i = tableList.iterator(); i.hasNext();) {
//					TableResult t = i.next();
//					// 表有权限进行字段权限判断
//					if (null != t && resources.contains(t.getTableId())) {
//						List<Column> columns = t.getColumns();
//						if (!CollectionUtils.isEmpty(columns)) {
//							for (Iterator<Column> j = columns.iterator(); j.hasNext();) {
//								Column column = j.next();
//								// 判断字段是否有权限，没权限删除
//								if (!resources.contains(String.valueOf(column.getMetricId()))) {
//									j.remove();
//								}
//							}
//							// 字段删光了，表删除
//							if (CollectionUtils.isEmpty(columns)) {
//								i.remove();
//							}
//						} else {
//							// 没有查询字段，删除
//							i.remove();
//						}
//					} else {
//						// 表没权限，删除
//						i.remove();
//					}
//				}
//			}
//		}
		Set<String> categorySet = new HashSet<String>();

		for (TableResult tableResult : tableList) {

			try {

				String categoryName = tableResult.getCategoryName();

				if (StringUtils.isNotBlank(categoryName)) {

					categorySet.add(categoryName);

				}

				if (!CollectionUtils.isEmpty(tableResult.getColumns())) {

					List<DataResult> filter = new ArrayList<DataResult>();

					List<DataResult> data = tableService.getData("t_" + tableResult.getTableNameEN(),
							tableResult.getColumns(), tybm);

						tableResult.setData(data);

				}
			} catch (Exception e) {

				ExceptionLogger.error(e);

			}

			// tableResult.setColumns(null);

		}

		result.put("results", tableList);

		result.put("category", StringUtils.join(categorySet.toArray(), " "));

		return result;

	}

}
